import {createRouter,createWebHistory} from 'vue-router';
import Login from '@/view/Login.vue'
import Register from '@/view/Register.vue'
import Home from '@/view/Home.vue'
import Main from '@/view/Main.vue'
import Recle from '@/view/Recle.vue'
import Imgfile from '@/view/Imgfile.vue'
import Music from '@/view/Music.vue';
import Video from '@/view/Video.vue';
import Doxc from '@/view/Doxc.vue'
import User from '@/view/User.vue'
import Phone from '@/view/Phone.vue' 
let router = createRouter({
    history:createWebHistory(),
    routes:[
        {
            path:"/login",
            component:Login
        },
        {
            path:"/register",
            component:Register
        },
        {
            path:"/phone",
            component:Phone
        },
        {
            path:"/home",
            component:Home,
            children:[
                {
                    path:"main",
                    component:Main
                },
                {
                    path:"imgfile",
                    component:Imgfile
                },
                {
                    path:"music",
                    component:Music
                },
                {
                    path:"video",
                    component:Video
                },
                {
                    path:"doxc",
                    component:Doxc
                },
                {
                    path:"recle",
                    component:Recle
                }
                ,
                {
                    path:"/home",
                    redirect:"/home/main"
                },
            ]
        },
        {
            path:"/user",
            component:User
        },
        {
            path:"/",
            redirect:"/login"
        }
    ]
})


router.beforeEach((to,from,next)=>{
    // console.log(to)
    // console.log(from)
    // console.log(next)
    if(to.path=="/login"||to.path=="/register"){
        next()
    }else{
        let token = localStorage.getItem("accessToken")
        if(token){
            next()
        }else{
            next("/login")
        }
    }
})

export default router